package com.baizhi.springbootday0101.service.impl;

import com.baizhi.springbootday0101.dao.BookDao;
import com.baizhi.springbootday0101.entity.Book;
import com.baizhi.springbootday0101.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BookServiceImpl implements BookService {

	@Autowired(required = false)
	private BookDao bookDao;
	@Override
	@Transactional(readOnly = true)
	public Map selectAll(int page, int limit, String name) {
		System.out.println("进入service的方法");
		Map map = new HashMap();
		if(name != null && !"".equals(name)){
			name = "%"+name+"%";
		}else{
			name = null;
		}

		int offset = (page-1)*limit;

		List<Book> books = bookDao.selectAll(offset, limit, name);
		int count = bookDao.selectCount();


		map.put("data",books);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");
		return map;
	}

	@Override
	@Transactional
	public Map insertBook(Book book) {
		Map map = new HashMap();
		int i = bookDao.insertBook(book);
		if(i > 0){
			map.put("isInsert","添加成功");
		}else{
			map.put("isInsert","添加失败");
		}
		return map;
	}

	@Override
	@Transactional
	public Map updateBook(Book book) {
		Map map = new HashMap();
		int i = bookDao.updateBook(book);
		if(i > 0){
			map.put("isUpdate","修改成功");
		}else{
			map.put("isUpdate","修改失败");
		}
		return map;
	}

	@Override
	@Transactional
	public Map deleteBook(int bookId) {
		Map  map = new HashMap();
		int i = bookDao.deleteBook(bookId);
		if(i > 0){
			map.put("isDelete","删除成功");
		}else{
			map.put("isDelete","删除失败");
		}
		return map;
	}
}
